<?php

namespace app\controller;

use think\Controller;
use think\Config;
use think\Lang;

class Base extends Controller
{
    protected $_site_name;
    protected $_site_domain;
    protected $_site_description;
    protected $_site_keywords;
    protected $_current_lang;
    protected $_current_lang_url;
    protected $_available_langs;

    public function _initialize()
    {
        Config::load(APP_PATH . 'extra/app.php');
    
        // 设置站点基本信息
        $this->_site_name = Lang::get('site_name');
        $this->_site_domain = Config::get('site_domain');
        $this->_site_description = Lang::get('site_description');
                
        // 多语言支持
        $this->_available_langs = Config::get('available_langs');
        
        $this->_current_lang = $this->detectLang();
        
        $this->_current_lang_url = get_current_lang_url($this->_current_lang);
        Lang::load(APP_PATH . 'lang/' . $this->_current_lang . '.php');
        
        
        // 检查浏览器语言
        $browser_lang = check_browser_language();
        $social_links = config('social_links');
        
        // 分配变量到模板
        $this->assign([
            'site_name' => $this->_site_name,
            'site_domain' => $this->_site_domain,
            'site_description' => $this->_site_description,
            'site_keywords' => $this->_site_keywords,
            'current_lang' => $this->_current_lang,
            'current_lang_url' => $this->_current_lang_url,
            'available_langs' => $this->_available_langs,
            'page_title' => '',
            'page_keywords' => '',
            'page_description' => '',
            'browser_lang' => $browser_lang,
            'social_links' => $social_links,
            
            'js_langs' => json_encode([
                'lang_toggle_both' => Lang::get('lang_toggle_both'),
                'lang_toggle_pinyin' => Lang::get('lang_toggle_pinyin'),
                'lang_toggle_chinese' => Lang::get('lang_toggle_chinese'),
                'search_progress' => [
                    'title' => Lang::get('searching_lyrics'),
                    'subtitle' => Lang::get('please_wait_searching'),
                    'searching' => Lang::get('searching'),
                    'percentage' => Lang::get('percentage_symbol'),
                    'complete' => Lang::get('search_complete'),
                    'texts' => [
                        Lang::get('analyzing_keywords'),
                        Lang::get('connecting_database'),
                        Lang::get('matching_songs'),
                        Lang::get('getting_lyrics'),
                        Lang::get('optimizing_results'),
                        Lang::get('preparing_display'),
                        Lang::get('almost_complete')
                    ]
                ],
                'search_buttons' => [
                    'search' => Lang::get('search'),
                    'searching' => Lang::get('searching_status')
                ],
                'lyric_detail' => [
                    'singer' => Lang::get('singer'),
                    'album' => Lang::get('album'),
                    'release_year' => Lang::get('release_year'),
                    'genre' => Lang::get('genre'),
                    'lyrics_content' => Lang::get('lyrics_content'),
                    'download' => Lang::get('download'),
                    'print' => Lang::get('print'),
                    'no_lyrics' => Lang::get('no_lyrics_content'),
                    'favorite' => Lang::get('favorite'),
                    'share' => Lang::get('share'),
                    'new_search' => Lang::get('new_search'),
                    'lang_toggle_both' => Lang::get('lang_toggle_both'),
                    'lang_toggle_pinyin' => Lang::get('lang_toggle_pinyin'),
                    'lang_toggle_chinese' => Lang::get('lang_toggle_chinese'),
                ],
                'no_results' => [
                    'title' => Lang::get('no_results_found'),
                    'subtitle' => Lang::get('no_results_subtitle'),
                    'suggestions_title' => Lang::get('search_suggestions'),
                    'suggestion_1' => Lang::get('check_spelling'),
                    'suggestion_2' => Lang::get('try_full_name'),
                    'suggestion_3' => Lang::get('try_artist_name'),
                    'suggestion_4' => Lang::get('try_lyrics_keywords'),
                    'try_again' => Lang::get('try_again'),
                    'random_song' => Lang::get('random_song'),
                    'hot_songs' => Lang::get('hot_songs')
                ],
                'messages' => [
                    'search_failed' => Lang::get('search_failed'),
                    'download_success' => Lang::get('download_success'),
                    'incomplete_info' => Lang::get('incomplete_info'),
                    'preparing_print' => Lang::get('preparing_print')
                ]
            ], JSON_UNESCAPED_UNICODE),
            
        ]);
    }

    /**
     * 检测语言
     */
    function detectLang() {
        $request = request();
        $path = $request->path();
        $lang_list = array_keys(Config::get('app.available_langs'));
        // 检查路径是否以语言代码开头
        foreach ($lang_list as $lang) {
            if ($path == '/') {
                return Config::get('app.default_lang');
            }else if (strpos($path, $lang) === 0) {
                return $lang;
            }
        }
        // 如果URL中没有语言标识，则返回默认语言
        return Config::get('app.default_lang');
    }

    /**
     * 切换语言
     */
    public function switchLang($lang = '')
    {
        if ($lang && in_array($lang, array_keys(Config::get('app.available_langs')))) {
            cookie('think_var', $lang);
        }
        $this->redirect($_SERVER['HTTP_REFERER'] ?? url('index/index'));
    }
    
    /**
     * 获取当前语言
     */
    public function getCurrentLang()
    {
        return $this->_current_lang;
    }
    
    /**
     * 获取语言包数据
     */
    public function getLangData($lang)
    {
        $lang_file = APP_PATH . 'lang/' . $lang . '.php';
        if (file_exists($lang_file)) {
            return include $lang_file;
        }
        return [];
    }
    
}